"""
1. AI智能体服务器的token
2. 上传文件到 AI智能体服务器
"""
import requests


def get_token():
    req = requests.post(
        url='http://172.18.0.66/dianda/uis/oauth2/token',
        headers={
            "Authorization": "Basic Z3Vlc3QtY2xpZW50OjEyMzQ1Ng==",
        },
        data={
            'grant_type': 'guest_anonymous'
        })
    data = req.json()
    token = f'{data["data"]["token_type"]} {data["data"]["access_token"]}'
    return token


def upload_file(local_file):
    token = get_token()
    headers = {
        "authorization": token
    }
    req = requests.post(
        url="http://172.18.0.66/dianda/fs/api/attachment/upload",
        headers=headers,
        files={'file': open(local_file, 'rb')}
    )
    data = req.json()
    file_url = f'http://172.18.0.66{data["data"]["url"]}'
    return file_url


if __name__ == '__main__':
    print(upload_file("/text2video/server.py"))
